package com.qin.entity.subcomponents;

import com.qin.entity.ExecuteBean;
import com.qin.entity.ExecuteLog;
import com.qin.service.ExecuteService;
import com.qin.service.impl.ExecuteServiceImpl;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/*
 * 【子组件】生成随机数
 */
public class RandomParaSubComponent {

    //变量名
    private String random_paraname;
    //随机数类型（0-数字0-9，1-字母a-z，2-字母A-Z）
    private int random_type;
    //位数
    private int random_digit;

    private ExecuteService executeService = new ExecuteServiceImpl();

    public RandomParaSubComponent(String random_paraname, int random_type, int random_digit) {
        this.random_paraname = random_paraname;
        this.random_type = random_type;
        this.random_digit = random_digit;
    }

    public void execute(ExecuteBean executeBean){

        //设置ExecuteLog对象的各属性
        ExecuteLog executeLog = new ExecuteLog(executeBean.getExecute_id(), executeBean.getFunction_id(),
                executeBean.getTestcase_id(), executeBean.getComponent_id(), executeBean.getSubcomponent_id(), "");

        String random_paravalue = null;
        StringBuffer stringBuffer = new StringBuffer();
        //String
        String boundStr = "";

        //0-数字0-9
        if(random_type == 0){
            boundStr = "0123456789";
        }
        //1-字母a-z
        else if(random_type == 1){
            boundStr = "abcdefghijklmnopqrstuvwxyz";
        }
        //2-字母A-Z
        else if(random_type == 2){
            boundStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        }

        Random random = new Random();
        for(int i=0; i<random_digit; i++){
            //生成[0, boundStr.length())的随机数
            int ranNum = random.nextInt(boundStr.length());
            stringBuffer.append(boundStr.substring(ranNum, ranNum+1));
        }
        random_paravalue = stringBuffer.toString();

        Map<String, String> retMap = new HashMap();
        retMap.put(random_paraname, random_paravalue);

        //插入日志
        executeService.insertExecuteLog(executeLog, ">>>>>生成随机数：变量名=" + random_paraname + "，变量值=" + random_paravalue);

        //把map的值添加到executeBean中
        executeBean.getParaMap().putAll(retMap);
    }

}
